﻿function isWxBrowser() {
    var result = false;
    //判断浏览器，在微信内打开，提示请在其他浏览器中打开
    var browser = {
        versions: function () {
            var u = navigator.userAgent, app = navigator.appVersion;
            return {
                //移动终端浏览器版本信息
                trident: u.indexOf('Trident') > -1, //IE内核
                presto: u.indexOf('Presto') > -1, //opera内核
                webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
                gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
                mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
                ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
                android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器
                iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
                iPad: u.indexOf('iPad') > -1, //是否iPad
                webApp: u.indexOf('Safari') == -1, //是否web应该程序，没有头部与底部,
                WeiXin: u.match(/MicroMessenger/i) == "MicroMessenger" //是否为微信客户端
            };
        }(),
        language: (navigator.browserLanguage || navigator.language).toLowerCase()
    };
    if (browser.versions.mobile) {
        //判断是否是移动设备打开。browser代码在下面
        var ua = navigator.userAgent.toLowerCase();
        //获取判断用的对象
        if (ua.match(/MicroMessenger/i) == "micromessenger") { //微信中打开
            return true;
        }
    }
    return result;
}
var browser = {
    versions: function () {
        var u = navigator.userAgent, app = navigator.appVersion;
        return {
            //移动终端浏览器版本信息
            trident: u.indexOf('Trident') > -1, //IE内核
            presto: u.indexOf('Presto') > -1, //opera内核
            webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
            gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
            mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
            ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
            android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器
            iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
            iPad: u.indexOf('iPad') > -1, //是否iPad
            webApp: u.indexOf('Safari') == -1, //是否web应该程序，没有头部与底部,
            WeiXin: u.match(/MicroMessenger/i) == "MicroMessenger" //是否为微信客户端
        };
    }(),
    language: (navigator.browserLanguage || navigator.language).toLowerCase()
};
//建立一個可存取到該file的url
function getObjectURL(file) {
    var url = null;
    if (window.createObjectURL != undefined) { // basic
        url = window.createObjectURL(file);
    }
    else if (window.URL != undefined) {
        // mozilla(firefox)
        url = window.URL.createObjectURL(file);
    }
    else if (window.webkitURL != undefined) {
        // webkit or chrome
        url = window.webkitURL.createObjectURL(file);
    }
    return url;
}


function isLogin() {
    try {
        if (browser.versions.ios) {
            window.webkit.messageHandlers.loginAction.postMessage('');
            return false;
        } else if (browser.versions.android) {
            androidfuntion.loginAction();
            return false;
        }
    } catch (e) {
        return true;
    }
    return true;
}
function show_share(img,title,desc,share_url) {
    var obj = {};
    obj.title = title;
    obj.desc =desc;
    obj.img = img;
    obj.url = share_url;
    try {
        var data = JSON.stringify(obj);
        if (browser.versions.ios) {
            window.webkit.messageHandlers.showShareButton.postMessage(data);
        } else if (browser.versions.android) {
            androidfuntion.showShareButton(data)
        }
    } catch (e) {
    }
    var obj2 = {};
    obj2.name = '';
    obj2.head_img_url = '';
    obj2.main_image_url = img;
    obj2.title = title;
    obj2.cost_price = '';
    obj2.price = '';
    obj2.invitation_code = '';
    obj2.type = 2;
    obj2.desc = desc;
    obj2.share_url = share_url;
    try {
        var data = JSON.stringify(obj2);
        if (browser.versions.ios) {
            window.webkit.messageHandlers.ShareProductType.postMessage(data);
        } else if (browser.versions.android) {
            androidfuntion.ShareProductType(data)
        }
    } catch (e) {
    }
}


//保留N位小数，如：2，还会保留2 不会补0
function toDecimalRoundNoZero(num, n) {
    var f = Math.round(num * Math.pow(10, n)) / Math.pow(10, n);
    var s = f.toString();
    return s;
}